<template>
  <el-container class="layout-container">
    <el-aside :width="isCollapse ? '64px' : '200px'" class="aside-container">
      <div class="logo">
        <el-icon><Monitor /></el-icon>
        <span v-if="!isCollapse">Summer Boss</span>
      </div>
      <menu-tree :is-collapse="isCollapse" @menu-select="handleMenuSelect"/>
    </el-aside>

    <el-container>
      <el-header>
        <div class="header-left">
          <el-icon class="collapse-btn" @click="isCollapse = !isCollapse">
            <Fold v-if="!isCollapse" />
            <Expand v-else />
          </el-icon>
        </div>
        <div class="header-right">
          <el-dropdown @command="handleCommand">
            <span class="user-info">
              <span class="username">{{ username || '未登录' }}</span>
              <el-icon class="el-icon--right"><arrow-down /></el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>

      <el-main>
        <router-view />
      </el-main>

      <el-footer>
        <div class="footer-content">
          <span>Copyright © 2025 Summer Boss. All rights reserved.</span>
        </div>
      </el-footer>
    </el-container>
  </el-container>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessageBox, ElMessage } from 'element-plus'
import {
  Monitor,
  Fold,
  Expand,
  ArrowDown
} from '@element-plus/icons-vue'
import { logout } from '../api/auth'
import MenuTree from '../components/MenuTree.vue'
import type { MenuItem } from '../types/menu'

const router = useRouter()
const username = ref(localStorage.getItem('username') || '')
const isCollapse = ref(false)

const handleMenuSelect = (menu: MenuItem) => {
  if (!menu.route && menu.type !== "目录") {
    ElMessage.info('该功能正在开发中，敬请期待')
    return
  }
  router.push(menu.route)
}

const handleCommand = async (command: string) => {
  if (command === 'logout') {
    await ElMessageBox.confirm('确定要退出登录吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(async () => {
      const result = await logout()
      if (result) {
        router.push('/login')
      }
    }).catch(() => {
      ElMessage.info('取消退出登录成功')
    })
  }
}
</script>

<style scoped>
.layout-container {
  height: 100vh;
}

.el-aside {
  background-color: var(--el-menu-bg-color);
  border-right: 1px solid var(--el-border-color-light);
}

.aside-container {
  transition: width 0.3s;
  overflow: hidden;
}

.logo {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--el-color-primary);
  font-size: 18px;
  font-weight: bold;
  border-bottom: 1px solid var(--el-border-color-light);
  white-space: nowrap;
  overflow: hidden;
}

.logo .el-icon {
  font-size: 24px;
  margin-right: 8px;
}

.el-header {
  background-color: var(--el-bg-color);
  border-bottom: 1px solid var(--el-border-color-light);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
}

.header-left {
  display: flex;
  align-items: center;
}

.collapse-btn {
  font-size: 20px;
  cursor: pointer;
  color: var(--el-text-color-primary);
}

.header-right {
  display: flex;
  align-items: center;
}

.user-info {
  display: flex;
  align-items: center;
  cursor: pointer;
  color: var(--el-text-color-primary);
  padding: 0 8px;
}

.user-info .username {
  margin: 0 8px;
  font-size: 14px;
  line-height: 1;
}

.user-info .el-icon--right {
  margin-left: 4px;
  font-size: 12px;
}

.el-main {
  background-color: var(--el-bg-color-page);
  padding: 20px;
}

.footer-content {
  text-align: center;
  color: var(--el-text-color-secondary);
  font-size: 14px;
  border-top: 1px solid var(--el-border-color-light);
}
</style> 